//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package admin_feedback_api_serv

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/admin_api_serv/admin_common"
	"atomgit.com/openlinksaas-org/api-server/config"
	"atomgit.com/openlinksaas-org/api-server/dao"
	"atomgit.com/openlinksaas-org/api-server/dao/user_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/feedback_api"
	"go.mongodb.org/mongo-driver/mongo"
)

type FeedBackAdminApiImpl struct {
	feedback_api.UnimplementedFeedBackAdminApiServer
	adminCfg *config.AdminCfg
}

func NewFeedBackAdminApiImpl(adminCfg *config.AdminCfg) *FeedBackAdminApiImpl {
	return &FeedBackAdminApiImpl{adminCfg: adminCfg}
}

func (apiImpl *FeedBackAdminApiImpl) List(ctx context.Context, req *feedback_api.AdminListRequest) (*feedback_api.AdminListResponse, error) {
	emptyList := []*feedback_api.FeedBackInfo{}
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &feedback_api.AdminListResponse{
			Code:     feedback_api.AdminListResponse_CODE_WRONG_SESSION,
			ErrMsg:   "会话不存在",
			InfoList: emptyList,
		}, nil
	}
	if !sess.AuthOk {
		return &feedback_api.AdminListResponse{
			Code:     feedback_api.AdminListResponse_CODE_NOT_AUTH,
			ErrMsg:   "未授权会话",
			InfoList: emptyList,
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, sess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.FeedBackPerm.Read {
		return &feedback_api.AdminListResponse{
			Code:     feedback_api.AdminListResponse_CODE_NO_PERMISSION,
			ErrMsg:   "没有权限",
			InfoList: emptyList,
		}, nil
	}

	count, err := dao.FeedBackDao.Count(ctx, false, "")
	if err != nil {
		return nil, err
	}
	feedBackItemList, err := dao.FeedBackDao.List(ctx, false, "", int64(req.Offset), int64(req.Limit))
	if err != nil {
		return nil, err
	}

	//补充用户信息
	userIdList := []string{}
	for _, feedBackItem := range feedBackItemList {
		if feedBackItem.UserId != "" {
			userIdList = append(userIdList, feedBackItem.UserId)
		}
	}
	userItemList, err := user_dao.UserInfoDao.ListById(ctx, userIdList)
	if err != nil {
		return nil, err
	}
	userItemMap := map[string]*user_dao.UserInfoDbItem{}
	for _, userItem := range userItemList {
		userItemMap[userItem.UserId] = userItem
	}

	//生成结果
	feedBackList := []*feedback_api.FeedBackInfo{}
	for _, feedBackItem := range feedBackItemList {
		feedBack := feedBackItem.ToFeedBackInfo()
		userItem, ok := userItemMap[feedBack.UserId]
		if ok {
			feedBack.UserDisplayName = userItem.BasicInfo.DisplayName
			feedBack.UserLogoUri = userItem.BasicInfo.LogoUri
		}
		feedBackList = append(feedBackList, feedBack)
	}

	return &feedback_api.AdminListResponse{
		Code:       feedback_api.AdminListResponse_CODE_OK,
		TotalCount: count,
		InfoList:   feedBackList,
	}, nil
}

func (apiImpl *FeedBackAdminApiImpl) Remove(ctx context.Context, req *feedback_api.AdminRemoveRequest) (*feedback_api.AdminRemoveResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &feedback_api.AdminRemoveResponse{
			Code:   feedback_api.AdminRemoveResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &feedback_api.AdminRemoveResponse{
			Code:   feedback_api.AdminRemoveResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, sess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.FeedBackPerm.Remove {
		return &feedback_api.AdminRemoveResponse{
			Code:   feedback_api.AdminRemoveResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = dao.FeedBackDao.ForceRemove(sessCtx, req.FeedBackId)
		if err != nil {
			return nil, err
		}
		return &feedback_api.AdminRemoveResponse{
			Code: feedback_api.AdminRemoveResponse_CODE_OK,
		}, nil
	})

	if err != nil {
		return nil, err
	}
	return ret.(*feedback_api.AdminRemoveResponse), nil
}
